package com.zzl.springboot_mybatis_demo.controller.book;

import com.gitee.yongzhuzl.commonutil.util.result.ResponseResult;
import com.zzl.springboot_mybatis_demo.entity.book.Book;
import com.zzl.springboot_mybatis_demo.service.book.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: 书籍Controller
 * @author 朱正磊
 * @date 2019-12-08 21:06:16
 */
@RestController
@RequestMapping("/api/book")
public class BookController {

    @Resource(name = "bookServiceImpl")
    private BookService bookService;

    /**
     * description: 根据主键查询唯一书籍
     * @author 朱正磊
     * @date 2019-12-08 21:36:35
     * @param id 主键
     * @return 返回响应对象
     */
    @GetMapping("/queryBookById/{id}")
    public ResponseResult queryBookById(@PathVariable Integer id) {
        try {
            Book book = bookService.queryBookById(id);
            return ResponseResult.buildSuccess(book);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.buildException();
        }
    }

    /**
     * description: 查询所有书籍
     * @author 朱正磊
     * @date 2019-12-08 21:36:41
     * @return 返回响应对象
     */
    @GetMapping("/queryAllBook")
    public ResponseResult queryAllBook() {
        try {
            List<Book> books = bookService.queryAllBook();
            return ResponseResult.buildSuccess(books);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.buildException();
        }
    }

}
